/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

import java.util.HashMap;
import java.util.Map;
import xowl.gmi.environment.Locale;

/**
 *
 * @author Laurent WOUTERS
 */
public class DialogNewDiagram extends javax.swing.JDialog {
    private static int width = 400;
    private static int height = 200;
    
    private boolean accept;
    private javax.swing.JComboBox comboProject;
    private javax.swing.JTextField fieldName;
    private javax.swing.JComboBox comboView;
    private Map<String, Object> mapProjects;
    private Map<String, Object> mapViews;
    
    public boolean isAccept() { return accept; }
    public Object getSelectedProject() { return mapProjects.get((String)comboProject.getSelectedItem()); }
    public String getSelectedName() { return fieldName.getText(); }
    public Object getSelectedView() { return mapViews.get((String)comboView.getSelectedItem()); }
    
    public DialogNewDiagram(Locale locale, javax.swing.JFrame frame, boolean canSelectProject) {
        super(frame);
        this.accept = false;
        this.mapProjects = new HashMap<>();
        this.mapViews = new HashMap<>();
        initComponents(locale, frame);
        comboProject.setEnabled(canSelectProject);
    }
    
    public void addProject(Object obj, String name) {
        mapProjects.put(name, obj);
        comboProject.addItem(name);
    }
    public void addWBView(Object obj, String name) {
        mapViews.put(name, obj);
        comboView.addItem(name);
    }
    public void prepare() {
        comboProject.setSelectedIndex(0);
        comboView.setSelectedIndex(0);
    }
    
    private void initComponents(Locale locale, javax.swing.JFrame mainFrame) {
        java.awt.Rectangle bounds = mainFrame.getBounds();
        int x = bounds.x + (bounds.width-width)/2;
        int y = bounds.y + (bounds.height-height)/2;
        this.setBounds(x, y, width, height);
        this.setModalityType(javax.swing.JDialog.ModalityType.APPLICATION_MODAL);
        this.setResizable(false);
        this.setTitle(locale.diagramNew);
        
        javax.swing.JLabel labelProject = new javax.swing.JLabel(locale.diagramProject, IconProvider.getEmbedded("project.png"), javax.swing.SwingConstants.LEFT);
        labelProject.setMinimumSize(new java.awt.Dimension(75, 25));
        labelProject.setPreferredSize(new java.awt.Dimension(75, 25));
        labelProject.setMaximumSize(new java.awt.Dimension(75, 25));
        
        javax.swing.JLabel labelName = new javax.swing.JLabel(locale.diagramName, IconProvider.getEmbedded("saveas.png"), javax.swing.SwingConstants.LEFT);
        labelName.setMinimumSize(new java.awt.Dimension(75, 25));
        labelName.setPreferredSize(new java.awt.Dimension(75, 25));
        labelName.setMaximumSize(new java.awt.Dimension(75, 25));
        
        javax.swing.JLabel labelView = new javax.swing.JLabel(locale.diagramView, IconProvider.getEmbedded("diagram.png"), javax.swing.SwingConstants.LEFT);
        labelView.setMinimumSize(new java.awt.Dimension(75, 25));
        labelView.setPreferredSize(new java.awt.Dimension(75, 25));
        labelView.setMaximumSize(new java.awt.Dimension(75, 25));
        
        comboProject = new javax.swing.JComboBox<>();
        comboProject.setMinimumSize(new java.awt.Dimension(275, 25));
        comboProject.setPreferredSize(new java.awt.Dimension(275, 25));
        comboProject.setMaximumSize(new java.awt.Dimension(275, 25));
        
        fieldName = new javax.swing.JTextField();
        fieldName.setMinimumSize(new java.awt.Dimension(275, 25));
        fieldName.setPreferredSize(new java.awt.Dimension(275, 25));
        fieldName.setMaximumSize(new java.awt.Dimension(275, 25));
        
        comboView = new javax.swing.JComboBox<>();
        comboView.setMinimumSize(new java.awt.Dimension(275, 25));
        comboView.setPreferredSize(new java.awt.Dimension(275, 25));
        comboView.setMaximumSize(new java.awt.Dimension(275, 25));
        
        javax.swing.JButton buttonOK = new javax.swing.JButton(locale.ok_text);
        buttonOK.setMinimumSize(new java.awt.Dimension(75, 25));
        buttonOK.setPreferredSize(new java.awt.Dimension(75, 25));
        buttonOK.setMaximumSize(new java.awt.Dimension(75, 25));
        buttonOK.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                if (comboProject.getSelectedItem() == null)
                    return;
                if (fieldName.getText() == null || "".equals(fieldName.getText()))
                    return;
                if (comboView.getSelectedItem() == null)
                    return;
                accept = true;
                setVisible(false);
            }
        });
        getRootPane().setDefaultButton(buttonOK);
        
        javax.swing.JButton buttonCancel = new javax.swing.JButton(locale.cancel_text);
        buttonCancel.setMinimumSize(new java.awt.Dimension(75, 25));
        buttonCancel.setPreferredSize(new java.awt.Dimension(75, 25));
        buttonCancel.setMaximumSize(new java.awt.Dimension(75, 25));
        buttonCancel.addActionListener(new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                accept = false;
                setVisible(false);
            }
        });
        getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0), "Cancel");
        getRootPane().getActionMap().put("Cancel", new javax.swing.AbstractAction() {
            @Override public void actionPerformed(java.awt.event.ActionEvent ae) {
                accept = false;
                setVisible(false);
            }
        });
        
        javax.swing.JPanel row1 = new javax.swing.JPanel();
        row1.setLayout(new javax.swing.BoxLayout(row1, javax.swing.BoxLayout.X_AXIS));
        row1.add(javax.swing.Box.createHorizontalStrut(10));
        row1.add(labelProject);
        row1.add(javax.swing.Box.createHorizontalStrut(10));
        row1.add(comboProject);
        row1.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel row2 = new javax.swing.JPanel();
        row2.setLayout(new javax.swing.BoxLayout(row2, javax.swing.BoxLayout.X_AXIS));
        row2.add(javax.swing.Box.createHorizontalStrut(10));
        row2.add(labelName);
        row2.add(javax.swing.Box.createHorizontalStrut(10));
        row2.add(fieldName);
        row2.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel row3 = new javax.swing.JPanel();
        row3.setLayout(new javax.swing.BoxLayout(row3, javax.swing.BoxLayout.X_AXIS));
        row3.add(javax.swing.Box.createHorizontalStrut(10));
        row3.add(labelView);
        row3.add(javax.swing.Box.createHorizontalStrut(10));
        row3.add(comboView);
        row3.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel row4 = new javax.swing.JPanel();
        row4.setLayout(new javax.swing.BoxLayout(row4, javax.swing.BoxLayout.X_AXIS));
        row4.add(javax.swing.Box.createHorizontalStrut(10));
        row4.add(buttonCancel);
        row4.add(javax.swing.Box.createHorizontalGlue());
        row4.add(buttonOK);
        row4.add(javax.swing.Box.createHorizontalStrut(10));
        
        javax.swing.JPanel content = (javax.swing.JPanel)getContentPane();
        content.setLayout(new javax.swing.BoxLayout(content, javax.swing.BoxLayout.Y_AXIS));
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row1);
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row2);
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row3);
        content.add(javax.swing.Box.createVerticalStrut(10));
        content.add(row4);
        content.add(javax.swing.Box.createVerticalStrut(10));
    }
}
